package com.czk.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.czk.cmn.feignclient.DictFeignClient;
import com.czk.model.hosp.Hospital;
import com.czk.repository.HospitalRepository;
import com.czk.service.HospitalService;
import com.czk.vo.hosp.HospitalQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/11/9 22:20
 */
@Service
public class HospitalServiceImpl implements HospitalService {
    @Resource
    private HospitalRepository hospitalRepository;
    @Autowired
    private DictFeignClient dictFeignClient;

    @Override
    public void save(Map<String, Object> paramMap) {
        // 判断是否存在相同的数
        String mapString = JSONObject.toJSONString(paramMap);
        // 转为json对象
        Hospital hospital = JSONObject.parseObject(mapString, Hospital.class);
        // 拿到医院代码
        String hoscode = hospital.getHoscode();
        Hospital hospitalExit = hospitalRepository.getHospitalByHoscode(hoscode);
        if (hospitalExit != null) {
            // 修改
            hospital.setStatus(hospitalExit.getStatus());
            hospital.setCreateTime(hospitalExit.getCreateTime());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
        } else {
            // 添加
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
        }
        hospitalRepository.save(hospital);
    }

    @Override
    public Hospital getByHoscode(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        return hospital;
    }

    /**
     * 分页模糊查询
     *
     * @param page
     * @param limit
     * @param hospitalQueryVo
     * @return
     */
    @Override
    public Page selectHospPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        PageRequest pageRequest = PageRequest.of(page - 1, limit);
        // 创建条件匹配器
        ExampleMatcher exampleMatcher = ExampleMatcher.matching()
                // mohuchxun1
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo, hospital);
        // 查询
        Example<Hospital> example = Example.of(hospital, exampleMatcher);
        Page<Hospital> pages = hospitalRepository.findAll(example, pageRequest);
        pages.getContent().forEach(this::setHospitalHosType);
        return pages;
    }

    private void setHospitalHosType(Hospital hospital) {
        // 获取医院类型
        String hostypeString = dictFeignClient.getName("Hostype", hospital.getHostype());
        // 获取省市
        String provinceString = dictFeignClient.getName(hospital.getProvinceCode());
        String cityString = dictFeignClient.getName(hospital.getProvinceCode());
        String districtString = dictFeignClient.getName(hospital.getDistrictCode());
        hospital.getParam().put("fullAddress", provinceString + cityString + districtString);
        hospital.getParam().put("hostypeString", hostypeString);
    }

    @Override
    public void updateHospStatus(String id, Integer status) {
        Optional<Hospital> byId = hospitalRepository.findById(id);
        if (byId.isPresent()) {
            Hospital hospital = byId.get();
            hospital.setStatus(status);
            hospital.setUpdateTime(new Date());
            hospitalRepository.save(hospital);
        }
    }

    @Override
    public Map<String,Object> getHospById(String id) {
        Optional<Hospital> optional = hospitalRepository.findById(id);
        Hospital hospital=new Hospital();
        if (optional.isPresent()) {
            hospital=optional.get();
        }
        this.setHospitalHosType(hospital);
        Map<String, Object> map=new HashMap<>();
        map.put("hospital",hospital);
        map.put("bookingRule",hospital.getBookingRule());
        // 不需要重复返回
        hospital.setBookingRule(null);
        return map;
    }

    @Override
    public String getHospName(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        if (hospital != null) {
            return hospital.getHosname();
        }
        return null;
    }

    @Override
    public List<Hospital> findByhHostName(String hosname) {
        List<Hospital> list=hospitalRepository.findHospitalByHosnameLike(hosname);
        return list;
    }

    @Override
    public Map<String, Object> item(String hoscode) {
        Map<String,Object> result=new HashMap<>();
        Hospital hospital = this.getByHoscode(hoscode);
        this.setHospitalHosType(hospital);
        result.put("hospital",hospital);
        result.put("bookingRule",hospital.getBookingRule());
        hospital.setBookingRule(null);
        return result;
    }
}
